package com.rovinj_server.fragment;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.rovinj_server.R;

import com.rovinj_server.adapter.InventorysScanningAdapter;
import com.rovinj_server.db.DBManager;
import com.rovinj_server.entity.InventorySub;
import com.rovinj_server.receiver.KeyReceiverListener;
import com.rovinj_server.utisl.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pda.serialport.Barcode1DManager;


/**
 * 扫描条码
 * 判断是否库位条码
 * 库位条码 查出 库位对应的  条码
 * 扫描到条码 判断是否 扫描到 库位条码 ， 没库位条码 提示
 * 扫描到条码 在对应款号 数量加1
 * 扫描到新条码 在全部单找出对应 款式 增加 集合
 * 存入数据库
 * 扫描到 已经扫描的提示
 * //////////////////////////////////////更改需求
 * <p>
 * 扫描 条码 显示 货位  ，货位对应的信息 数量 显示
 * <p>
 * 再次扫描条码 判断 是否相同货位 ，不相同货位提示， 相同货号 数量增加
 * 提示 可以在这个货位增加  可以取消
 * <p>
 * <p>
 * 我改扫描 条码  显示 对应货位号 ，扫描到其他货位的条码 提示或者数量过多提示，这个功能，  演示给你看看与你们之前的盘点有没好处，或者要改进的地方 在做 后面那个功能
 * <p>
 * ------------------------------------再次更改
 */

public class FragmentInventorySaninngTEXT extends Fragment implements KeyReceiverListener {
    public static final String TAG = "-----FragmentInventorySaninng";
    @Bind(R.id.disc_number)
    TextView mDiscNumber;
    @Bind(R.id.sao_sj_qty)
    TextView mSaoSjQty;
    @Bind(R.id.location)
    EditText mLocation;
    @Bind(R.id.butt_saninng)
    Button mButtSaninng;
    @Bind(R.id.but_chongsao)
    Button mButChongsao;
    @Bind(R.id.recycler_view_m)
    RecyclerView mRecyclerViewM;
    @Bind(R.id.ll_butt)
    LinearLayout mLlButt;
    @Bind(R.id.but_submit)
    Button mButSubmit;
    private Context mContext;
    private View mView;
    //  private SweetAlertDialog dialog;
    private List<InventorySub> mListSubData = new ArrayList<>();
    private List<InventorySub> mDataList = new ArrayList<>();
    private boolean booleanScainng = false;
    private int mId;
    private Barcode1DManager manager;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == Barcode1DManager.Barcode1D) {
                String str = msg.getData().getString("data").toString();
                String data = str.replace("\r\n", "");
                Log.d(TAG, "--条码--" + data + "-----" + data.length());
                Util.play(1, 0);
                if (data != null && data.length() > 0) {
                    //  yzBarode(data);
                    //   get_Barode_location(data);
                    barcode_Validation(data);
                } else {
                    Toast.makeText(mContext, "请继续扫描条码", Toast.LENGTH_SHORT).show();
                }

            }
            switch (msg.what) {
                case 0:   // 获取货位
                    if (mDataList != null && mDataList.size() > 0) {
                        mAdapter.setList(mDataList);
                        mAdapter.notifyDataSetChanged();
                        Log.d(TAG, "-根据条码查询数据mDataList--what ");
                    }

                    break;
                case 1:
                    mProgressDialog.dismiss();
                    mDataList.clear();
                    mAdapter.setList(mDataList);
                    mAdapter.notifyDataSetChanged();
                    mDiscNumber.setText("0");
                    mSaoSjQty.setText("0");
                    mLocation.setText("");
                    mButSubmit.setEnabled(false);
                    mButChongsao.setEnabled(false);
                    Toast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    break;
            }
        }
    };
    private InventorysScanningAdapter mAdapter;
    private DBManager mManager;
    private ProgressDialog mProgressDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = View.inflate(mContext, R.layout.fragment_inventory_scanning, null);
        }
        ButterKnife.bind(this, mView);
        manager = new Barcode1DManager();
        manager.Open(mHandler);
        Util.initSoundPool(mContext);
        // ini();
        return mView;
    }


    @Override
    public void onStart() {
        super.onStart();
        ini();
    }

    private void ini() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mRecyclerViewM.setLayoutManager(linearLayoutManager);
        mAdapter = new InventorysScanningAdapter(mDataList);
        mRecyclerViewM.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
        Bundle arguments = getArguments();
        if (arguments != null) {
            mId = arguments.getInt("id");
            Log.d(TAG, mId + "");
        }
        mManager = new DBManager(mContext);
        mManager.copyDBFile();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 验证条码
     */
    private void get_Barode_location(final String data)  {
        ButterKnife.bind(this, mView);
        int qty = 0;
        int discQty = 0;
        try {
            set_showDialog();
        } catch (Exception e) {
            e.printStackTrace();
        }

        String location = "";
        //扫描到条码 查询 货位 显示
        InventorySub barodeSub = mManager.findBarodeSub(data);
        if (barodeSub != null) {
            location = barodeSub.getLocation();
        }

        if (mLocation.getText().toString().equals("")) {  //第一次扫描 为空就设置
            mLocation.setText(location);
            get_qty(); //判断是否数量过多

            //根据 货位查询出 所有条码
            mDataList = mManager.findLocationSubListData(location);
            if (mDataList != null && mDataList.size() > 0) {
                for (int i = 0; i < mDataList.size(); i++) {
                    int sj = Integer.parseInt(mDataList.get(i).getSjnumber());
                    qty = qty + sj;
                    int discNumber = mDataList.get(i).getDiscNumber();
                    discQty = discQty + discNumber;
                    Log.d(TAG, "-根据条码查询数据mDataList-- " + i);
                }
                mSaoSjQty.setText((qty + ""));
                mDiscNumber.setText((discQty + ""));
                mAdapter.setList(mDataList);
                mButSubmit.setEnabled(true);
                mButChongsao.setEnabled(true);
                mAdapter.notifyDataSetChanged();
                Log.d(TAG, "-根据条码查询数据mDataList--qty " + qty);

                for (int i = 0; i < mDataList.size(); i++) { //在扫描当前库位 查找条码
                    String barode = mDataList.get(i).getBarode();
                    if (barode.equals(data)) {
                        mDataList.get(i).setDiscNumber(mDataList.get(i).getDiscNumber() + 1);
                        final int disc = Integer.parseInt(mDiscNumber.getText().toString());
                        mManager.updateQtyData(barode, "已盘");
                        mDiscNumber.setText((disc + 1) + "");
                        mAdapter.notifyDataSetChanged();
                        break;

                    }
                }

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        mProgressDialog.dismiss();
                    }
                }).start();

            }
        } else { //第2次扫描
            get_qty(); //判断数量
            boolean isTrue = false;
            if (location.equals(mLocation.getText().toString())) { //如果 第2次 货号和 显示货位相同
                for (int i = 0; i < mDataList.size(); i++) { //在扫描当前库位 查找条码
                    String barode = mDataList.get(i).getBarode();
                    if (barode.equals(data)) {
                        mDataList.get(i).setDiscNumber(mDataList.get(i).getDiscNumber() + 1);
                        final int disc = Integer.parseInt(mDiscNumber.getText().toString());
                        mManager.updateQtyData(barode,"已盘");
                        isTrue = true;
                        mDiscNumber.setText((disc + 1) + "");
                        mAdapter.notifyDataSetChanged();
                        break;
                    }
                }
                set_dismissColse();
            } else { //如果扫描货位不相同 提示
                mButtSaninng.setEnabled(false);
                //播放提示音
                if (location == null || location.equals("")) {
                    location = "不存在";
                }
                String str = "款式货位号：" + location + "     非本货位款式，请处理！ ";
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("提示");
                builder.setMessage(str);
                builder.setPositiveButton("取消", null).create();
                builder.setNegativeButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        boolean isTrue = false;
                        for (int i = 0; i < mDataList.size(); i++) { //在扫描当前库位 查找条码
                            String barode = mDataList.get(i).getBarode();
                            if (barode.equals(data)) {
                                mDataList.get(i).setDiscNumber(mDataList.get(i).getDiscNumber() + 1);
                                final int disc = Integer.parseInt(mDiscNumber.getText().toString());
                                mManager.updateQtyData(barode, "已盘");
                                isTrue = true;
                                mDiscNumber.setText((disc + 1) + "");
                                mAdapter.notifyDataSetChanged();
                                break;
                            }
                        }
                        if (!isTrue) {
                            InventorySub barodeSub = mManager.findBarodeSub(data);
                            if (barodeSub != null) {
                                barodeSub.setDiscNumber(1);
                                barodeSub.setSjnumber(0 + "");
                                mManager.insertSubData(barodeSub);
                                mDataList.add(0, barodeSub);
                                int disc = Integer.parseInt(mDiscNumber.getText().toString());
                                mDiscNumber.setText((disc + 1) + "");
                                mAdapter.notifyDataSetChanged();
                            }
                        } else {
                            Toast.makeText(mContext, "非法条码", Toast.LENGTH_SHORT).show();
                        }
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                mProgressDialog.dismiss();
                            }
                        }).start();
                        mButtSaninng.setEnabled(true);
                        dialog.dismiss();
                    }
                }).create();

                builder.setCancelable(false);
                builder.show();

            }

        }


        //根据货位 查询所有 条码


        //


        set_dismissColse();

    }

    private void get_qty() {

        int zongQty = Integer.parseInt(mSaoSjQty.getText().toString());
        int yiQty = Integer.parseInt(mDiscNumber.getText().toString());
        if (yiQty > zongQty) {
            Toast.makeText(mContext, "数量过多", Toast.LENGTH_SHORT).show();
        }
        if (mDataList != null && mDataList.size() > 0) {
            for (int i = 0; i < mDataList.size(); i++) {
                int siNumber = Integer.parseInt(mDataList.get(i).getSjnumber());//实际数
                int discNumber = mDataList.get(i).getDiscNumber();  //盘点数
                if (discNumber > siNumber) {
                    validation_dialog();
                    break;
                } else {
                    getBarcode();
                }
            }
        } else {
            getBarcode();
        }

    }

    private void set_dismissColse() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mProgressDialog.dismiss();
            }
        }).start();
    }


    private void yzBarode(final String data) {
        ButterKnife.bind(this, mView);
        try {
            mProgressDialog = ProgressDialog.show(getActivity(), "", "正在验证数据请稍等");
            mProgressDialog.setCancelable(false);
            mProgressDialog.setIndeterminate(true);
            mProgressDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int qty = 0;
        int discQty = 0;
        if (data != null && data.length() > 0) {
            if (data.length() == 18) { //获取条码 更新数量
                String location = null;
                try {
                    location = mLocation.getText().toString();
                } catch (Exception e) {

                    e.printStackTrace();
                }
                boolean isTrue = false;
                if (location != null && location.length() > 0) {
                    for (int i = 0; i < mDataList.size(); i++) { //在扫描当前库位 查找条码
                        String barode = mDataList.get(i).getBarode();
                        if (barode.equals(data)) {
                            mDataList.get(i).setDiscNumber(mDataList.get(i).getDiscNumber() + 1);
                            final int disc = Integer.parseInt(mDiscNumber.getText().toString());
                            mManager.updateQtyData(barode, "已盘");
                            isTrue = true;
                            mDiscNumber.setText((disc + 1) + "");
                            mAdapter.notifyDataSetChanged();
                            break;
                        }
                    }

                    if (!isTrue) {
                        mListSubData = mManager.findIdBarodeSubListData(mId, data);
                        if (mListSubData != null && mListSubData.size() > 0) {
                            for (int j = 0; j < mListSubData.size(); j++) { //去 全部 库位找
                                String barodeList = mListSubData.get(j).getBarode();
                                if (data.equals(barodeList)) {
                                    InventorySub is = mListSubData.get(j);
                                    InventorySub inventorySub = new InventorySub();
                                    inventorySub.setLocation(location);
                                    inventorySub.setBarode(is.getBarode());
                                    inventorySub.setGoods(is.getGoods());
                                    inventorySub.setGoodsName(is.getGoodsName());
                                    inventorySub.setType(is.getType());
                                    inventorySub.setMaitype(is.getMaitype());
                                    inventorySub.setCode(is.getCode());
                                    inventorySub.setSizeName(is.getSizeName());
                                    inventorySub.setCheadguid(is.getCheadguid());
                                    inventorySub.setDiscNumber(1);
                                    inventorySub.setSjnumber(0 + "");
                                    mManager.insertSubData(inventorySub);
                                    mDataList.add(0, inventorySub);
                                    int disc = Integer.parseInt(mDiscNumber.getText().toString());
                                    mDiscNumber.setText((disc + 1) + "");
                                    mAdapter.notifyDataSetChanged();
                                    break;
                                }
                            }
                        } else {

                            Toast.makeText(mContext, "非法条码", Toast.LENGTH_SHORT).show();

                        }
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                mProgressDialog.dismiss();
                            }
                        }).start();
                    }
                } else {

                    Toast.makeText(mContext, "请扫描库位", Toast.LENGTH_SHORT).show();

                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        mProgressDialog.dismiss();
                    }
                }).start();


            } else {  //获取货位
                boolean tyme = false;
                mDataList.clear();
                try {
                    mManager = new DBManager(mContext);
                    mManager.copyDBFile();
                    mDataList = mManager.findIdLocationSubListData(mId, data);
                    Log.d(TAG, "-根据条码查询数据mDataList--" + mDataList.size());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (mDataList != null && mDataList.size() > 0) {
                    for (int i = 0; i < mDataList.size(); i++) {
                        int sj = Integer.parseInt(mDataList.get(i).getSjnumber());
                        qty = qty + sj;
                        int discNumber = mDataList.get(i).getDiscNumber();
                        discQty = discQty + discNumber;
                        Log.d(TAG, "-根据条码查询数据mDataList-- " + i);
                    }
                    mSaoSjQty.setText((qty + ""));
                    mLocation.setText(data);
                    mDiscNumber.setText((discQty + ""));
                    mAdapter.setList(mDataList);
                    mAdapter.notifyDataSetChanged();
                    Log.d(TAG, "-根据条码查询数据mDataList--qty " + qty);

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            mProgressDialog.dismiss();
                        }
                    }).start();

                } else {

                    Toast.makeText(mContext, "非法库位条目", Toast.LENGTH_SHORT).show();
                }

            }

        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                mProgressDialog.dismiss();
            }
        }).start();

    }

    private void getBarcode() {
        if (mButtSaninng.getText().toString().equals("扫描")) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    manager.Scan();
                }
            }).start();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.butt_saninng, R.id.but_chongsao, R.id.but_submit, R.id.rr_lmg, R.id.butt_eliminate})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.butt_saninng:
                if (booleanScainng) {
                    booleanScainng = false;
                    getBarcode();
                } else {
                    get_qty();
                }

                //
                break;
            case R.id.but_chongsao:
                mLocation.setText("");
                for (int i = 0; i < mDataList.size(); i++) {
                    mManager.updateQtyNull(mDataList.get(i).getBarode(),mDataList.get(i).getCode());
                }
                mDataList.clear();
                mAdapter.setList(mDataList);
                mAdapter.notifyDataSetChanged();
                mDiscNumber.setText("0");
                mSaoSjQty.setText("0");
                mButChongsao.setEnabled(false);
                mButSubmit.setEnabled(false);
                Toast.makeText(mContext, "清空数据成功", Toast.LENGTH_SHORT).show();
                break;

            case R.id.but_submit:
                String qty = mSaoSjQty.getText().toString();
                if (qty.equals("0")) {
                    Toast.makeText(mContext, "请扫描条码", Toast.LENGTH_SHORT).show();
                } else {
                    dialog();
                }

            case R.id.rr_lmg:
                if (mLocation.getText().toString().equals("")) {
                    set_Toast("暂无数据");
                    ui_Updata("", "", "清空数据");
                } else {
                    ui_Updata(mLocation.getText().toString(), "", "货位信息");
                }
                break;
            case R.id.butt_eliminate:
                ui_Updata("", "", "清空数据");
                break;
        }
    }

    /**
     * 弹出对话框提示是否确定要重扫
     */
    private void dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage("提交任务吗？");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                set_showDialog();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        List<InventorySub> stateSubDataList = mManager.findStateSubDataList("已盘"); //查询出状态为已盘 数据
                        mManager.insertTsakSubData(stateSubDataList); //提交已盘数据
                        mHandler.sendEmptyMessage(1);
                    }
                }).start();

                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    private void set_showDialog() {
        mProgressDialog = ProgressDialog.show(mContext, "", "正在提交数据请稍等");
        mProgressDialog.setCancelable(false);
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.show();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        int keyCode = intent.getIntExtra("keyCode", 0);
        Log.d(TAG, "按了按键，keycode是=" + intent.getIntExtra("keyCode", 0));
        switch (keyCode) {
            case 133:
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mButtSaninng.isEnabled()) {
                            mButtSaninng.performClick();
                        }
                    }
                });
                break;
        }
    }


    /*****
     * 验证条码
     */
    public void barcode_Validation(String strBarcode) {
        ButterKnife.bind(this, mView);
        //  set_showDialog();

        String locationTv = mLocation.getText().toString();
        //  第一次扫描 ：扫描条码 识别是否是货位
        if (locationTv.equals("")) {
            if (strBarcode.length() < 10) {
                //     1. 是货位  查询出 货位对应 款号显示  ，所有存入集合List 显示
                ui_Updata(strBarcode, "", "货位信息");
            } else {
                //  2. 不是货位是条码：按照条码 查出货位 ，在查出对应 款式显示，所有存入集合List。
                InventorySub barodeSub = mManager.findBarodeSub(strBarcode);
                if (barodeSub != null) {
                    String locationSub = barodeSub.getLocation();
                    if (locationSub == null) {
                        set_Toast("暂无查到改条码货位信息");
                    } else {
                        ui_Updata(locationSub, strBarcode, "第一次扫描");
                    }
                } else {
                    set_Toast("暂无查到改条码货位信息");
                }


            }
        } else {


            //    第2次扫描：扫描条码
            if (strBarcode.length() < 10) {    //判断第2次扫描是否 扫描到 货位 还是条码
                //   1如果为货位是否与显示货位相同
                if (locationTv.equals(strBarcode)) {
                    if (mDataList == null && mDataList.size() < 0) {
                        //更新UI
                        ui_Updata(strBarcode, "", "货位信息");
                    }
                } else {
                    //   不相同更换货位显示，查询款式存入集合list显示


                    //更新UI
                    ui_Updata(strBarcode, "", "货位信息");
                }

            } else {
                //    2 如果 为条码
                //    在 list集合对应条码数量增加
                //    2 如果集合没对应条码 查询改条码 对应 货位 提示用户。
                if (mDataList == null && mDataList.size() < 0) {
                    ui_Updata(locationTv, strBarcode, "货位信息");
                }
                if (mDataList != null && mDataList.size() > 0) {
                    ui_Updata("", strBarcode, "第二次扫描");
                }

            }

        }
    }

    private void ui_Updata(String locationStr, final String barodeStr, String str) {

        int qty = 0;
        int discQty = 0;
        if (str.equals("清空数据")) {
            mDataList.clear();
            mSaoSjQty.setText(("0"));
            mDiscNumber.setText(("0"));
            mLocation.setText("");
            mAdapter.setList(mDataList);
            mAdapter.notifyDataSetChanged();
            return;
        } else if (str.equals("货位信息")) {
            mButSubmit.setEnabled(true);
            mButChongsao.setEnabled(true);
            mLocation.setText(locationStr);
            mDataList.clear();
            mDataList = mManager.findLocationSubListData(locationStr);
            if (mDataList != null && mDataList.size() > 0) {
                for (int i = 0; i < mDataList.size(); i++) {
                    int sj = Integer.parseInt(mDataList.get(i).getSjnumber());
                    qty = qty + sj;
                    int discNumber = mDataList.get(i).getDiscNumber();
                    discQty = discQty + discNumber;
                    Log.d(TAG, "-根据条码查询数据  货位信息mDataList-- " + i);
                }
                mSaoSjQty.setText((qty + ""));
                mDiscNumber.setText((discQty + ""));
            } else {
                //提示 用户 没找到对应 货位信息
                set_Toast("----暂无找到该货位信息");
                mLocation.setText("");
                mSaoSjQty.setText(("0"));
                mDiscNumber.setText(("0"));
            }
            mAdapter.setList(mDataList);
            mAdapter.notifyDataSetChanged();
            return;
        } else if (str.equals("第一次扫描")) {
            mLocation.setText(locationStr);
            mButSubmit.setEnabled(true);
            mButChongsao.setEnabled(true);
            //根据 货位查询出 所有条码
            mDataList = mManager.findLocationSubListData(locationStr);
            if (mDataList != null && mDataList.size() > 0) {
                for (int i = 0; i < mDataList.size(); i++) {
                    int sj = Integer.parseInt(mDataList.get(i).getSjnumber());
                    qty = qty + sj;
                    int discNumber = mDataList.get(i).getDiscNumber();
                    discQty = discQty + discNumber;
                    Log.d(TAG, "-根据条码查询数据mDataList-- " + i);
                }
                mSaoSjQty.setText((qty + ""));
                mDiscNumber.setText((discQty + ""));
                mAdapter.setList(mDataList);
                mButSubmit.setEnabled(true);
                mButChongsao.setEnabled(true);
                mAdapter.notifyDataSetChanged();
                Log.d(TAG, "-根据条码查询数据mDataList--qty " + qty);
                for (int i = 0; i < mDataList.size(); i++) { //在扫描当前库位 查找条码
                    String barode = mDataList.get(i).getBarode();
                    if (barode.equals(barode)) {
                        mDataList.get(i).setDiscNumber(mDataList.get(i).getDiscNumber() + 1);
                        final int disc = Integer.parseInt(mDiscNumber.getText().toString());
                        mManager.updateQtyData(barode, "已盘");
                        mDiscNumber.setText((disc + 1) + "");
                        mAdapter.notifyDataSetChanged();
                        break;
                    }
                }
            }

            return;
        } else if (str.equals("第二次扫描")) {
            boolean isTrue = false;
            mButSubmit.setEnabled(true);
            mButChongsao.setEnabled(true);
            for (int i = 0; i < mDataList.size(); i++) { //在扫描当前库位 查找条码 有就更新数据
                String barode = mDataList.get(i).getBarode();
                if (barode.equals(barodeStr)) {
                    mDataList.get(i).setDiscNumber(mDataList.get(i).getDiscNumber() + 1);
                    final int disc = Integer.parseInt(mDiscNumber.getText().toString());
                    mManager.updateQtyData(barode, "已盘");
                    isTrue = true;
                    mDiscNumber.setText((disc + 1) + "");
                    mAdapter.notifyDataSetChanged();
                    return;
                }
            }
            // 如果不在 当前货位 去所有货位查询
            InventorySub barodeSub = mManager.findBarodeSub(barodeStr);
            if (barodeSub == null) {
                set_Toast("款式没对应货位");
                return;
            }
            String location = barodeSub.getLocation();
            if (mLocation.getText().toString().equals(location)) {

            } else {
                //播放提示音
                if (location == null || location.equals("")) {
                    location = "不存在";
                }
                String strDialog = "款式货位号：" + location + "     非本货位款式，请处理！ ";
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("提示");
                builder.setMessage(strDialog);
                builder.setPositiveButton("取消", null).create();
                builder.setNegativeButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        boolean isTrue = false;
                        for (int i = 0; i < mDataList.size(); i++) { //在扫描当前库位 查找条码
                            String barode = mDataList.get(i).getBarode();
                            if (barode.equals(barodeStr)) {
                                mDataList.get(i).setDiscNumber(mDataList.get(i).getDiscNumber() + 1);
                                final int disc = Integer.parseInt(mDiscNumber.getText().toString());
                                mManager.updateQtyData(barode, "已盘");
                                isTrue = true;
                                mDiscNumber.setText((disc + 1) + "");
                                mAdapter.notifyDataSetChanged();
                                break;
                            }
                        }
                        if (!isTrue) {
                            InventorySub barodeSub = mManager.findBarodeSub(barodeStr);
                            if (barodeSub != null) {
                                //  barodeSub.setBeizhu(mLocation.getText().toString());
                                barodeSub.setDiscNumber(1);
                                barodeSub.setSjnumber(0 + "");
                                mManager.insertSubData(barodeSub);
                                mDataList.add(0, barodeSub);
                                int disc = Integer.parseInt(mDiscNumber.getText().toString());
                                mDiscNumber.setText((disc + 1) + "");
                                mAdapter.notifyDataSetChanged();
                            }
                        } else {
                            Toast.makeText(mContext, "非法条码", Toast.LENGTH_SHORT).show();
                        }
                        mButtSaninng.setEnabled(true);
                        dialog.dismiss();
                    }
                }).create();

                builder.setCancelable(false);
                builder.show();

            }
        }
    }

    public void set_Toast(final String m_Toast) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(mContext, "" + m_Toast, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 弹出对话框提示是否确定要重扫
     */
    private void validation_dialog() {
        mButtSaninng.setEnabled(false);
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("提示");
        builder.setMessage("数量过多");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                booleanScainng = true;
                mButtSaninng.setEnabled(true);

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                mButtSaninng.setEnabled(true);
            }
        });
        builder.create().show();
    }
}
